package com.fingard.dsp.bank.directbank.lzcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class LZCB01Balance extends LZCB01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        try {
            LZCB01Header bankHeader = new LZCB01Header();
            bankHeader.transCode = "B2ECSTZActBalQry";
            bankHeader.reqSeqNo = p_req7506.reqHeader.transSeqID;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("Body");
            while (p_req7506.readRecord()) {
                xmlWriter.writeElementString("AcNo", p_req7506.readedDetail.actNum);
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] retStrArr = sendToBank(xmlWriter.toXmlString(),bankHeader);
            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("Message/Head/IBSReturnMsg");
                    if ("000000".equals(tmpRetCode)) {
                        String repCode = tmpBkReader.getFirstText("/Message/Body/ReturnCode");
                        String repInfo = tmpBkReader.getFirstText("Message/Body/ReturnMsg");
                        if(repCode!=null && repCode.equals("000000")) {
                            while (tmpBkReader.read("/Message/Body")) {
                                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.respInfo = repInfo;
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AcNo");
                                tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("Currency"));
                                tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Balance");
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("AvailBal");
                                //无冻结余额
                                //tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleText("freeze_bal");
                                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                            }
                        } else {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = repInfo;
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }
                    } else {
                        RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpRetInfo;
                        retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(retStrArr[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
